package cn.zn.modules.zzsys.cur.controller;

import cn.zn.framework.common.utils.R;
import cn.zn.modules.zzsys.cur.entity.TeacherPgroup;
import cn.zn.modules.zzsys.cur.entity.dto.UserProcessGroupSaveDTO;
import cn.zn.modules.zzsys.cur.service.TeacherPgroupDeptService;
import cn.zn.modules.zzsys.cur.service.TeacherPgroupService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author ZN
 */
@RestController
@RequestMapping("zzsys/teacherpgroup")
@Api(tags = "流程用户组")
public class TeacherPgroupController {

    @Autowired
    private TeacherPgroupService teacherPgroupService;
    @Autowired
    private TeacherPgroupDeptService teacherPgroupDeptService;

    /**
     * 设置列表
     */
    @PostMapping("/list")
    @ApiOperation("查询流程用户组设置列表")
    public R list() {
        return R.ok().put("list", teacherPgroupService.queryGroupList());
    }

    @GetMapping("/userList/{processGroupCode}")
    @ApiOperation("查询流程用户组用户列表")
    public R userList(@PathVariable String processGroupCode) {
        return R.ok().put("list", teacherPgroupService.queryUserGroupList(processGroupCode));
    }

    @GetMapping("/userDeptList/{processGroupCode}")
    @ApiOperation("查询流程用户组用户部门列表")
    public R userDeptList(@PathVariable String processGroupCode) {
        return R.ok().put("list", teacherPgroupService.queryUserDeptGroupList(processGroupCode));
    }

    @PostMapping("/saveForDeptNew")
    @ApiOperation("保存部门新增")
    public R saveForDeptNew(@Valid @RequestBody UserProcessGroupSaveDTO dto) {
        teacherPgroupService.saveUserProcessGroup(dto, false, false);
        return R.ok();
    }

    @PostMapping("/saveForDeptInstead")
    @ApiOperation("保存部门替换")
    public R saveForDeptInstead(@Valid @RequestBody UserProcessGroupSaveDTO dto) {
        teacherPgroupService.saveUserProcessGroup(dto, true, false);
        return R.ok();
    }

    @PostMapping("/saveForProcessGroupNew")
    @ApiOperation("保存流程角色新增")
    public R saveForProcessGroupNew(@Valid @RequestBody UserProcessGroupSaveDTO dto) {
        teacherPgroupService.saveUserProcessGroup(dto, false, false);
        return R.ok();
    }

    @PostMapping("/saveForProcessGroupInstead")
    @ApiOperation("保存流程角色替换")
    public R saveForProcessGroupInstead(@Valid @RequestBody UserProcessGroupSaveDTO dto) {
        teacherPgroupService.saveUserProcessGroup(dto, false, true);
        return R.ok();
    }

    @DeleteMapping("/deleteById")
    @ApiOperation("根据ID删除")
    public R deleteById(@RequestBody List<String> ids) {
        teacherPgroupService.removeByIds(ids);
        return R.ok();
    }

    @DeleteMapping("/deleteByDept/{processGroupCode}/{deptId}")
    @ApiOperation("根据部门删除")
    public R deleteByDept(@PathVariable String processGroupCode, @PathVariable String deptId) {
        teacherPgroupService.remove(new QueryWrapper<TeacherPgroup>()
                .eq("GROUP_", processGroupCode)
                .in("T_SYS_DEPT_ID_", deptId));
        return R.ok();
    }

    @PostMapping("/pgroupdept/save/{id}")
    @ApiOperation("保存工作流角色对应的部门集合")
    public void pgroupdeptSave(@PathVariable String id, @RequestBody String[] deptIds) {
        teacherPgroupDeptService.pgroupdeptSave(id, deptIds);
    }
}
